package com.exigen.performance.test.webapp.dao;

import javax.persistence.Query;

import com.exigen.performance.test.webapp.domain.DashboardEntity;
import com.exigen.performance.test.webapp.domain.base.GenericJPADAO;

/**
 * Class responsible for dash board entity collection from database and dash
 * board data update.
 * 
 * @author Maksim Savchuk
 * 
 */
public class DashboardJpaDao extends GenericJPADAO<DashboardEntity, Long>
		implements DashboardDao {

	String query1 = "update PERF_DASHBOARD_PROJECTS d "
			+ "SET OVERALL_COUNT =( "
			+ "select count (distinct scenario_name || ' ' || scenario_type_name) "
			+ "from perf_testsummary ts "
			+ "Inner join perf_scenario sc on sc.scenario_id = ts.scenario_id "
			+ "inner join perf_projectname p on p.project_id=ts.project_id "
			+ "inner join perf_scenario_type st on st.scenario_type_id = ts.scenario_type_id "
			+ "where p.project_id=d.PROJECT_ID), "
			+ "EXECUTED_COUNT =( "
			+ "select count (distinct scenario_name || ' ' || scenario_type_name) "
			+ "from perf_testsummary ts "
			+ "Inner join perf_scenario sc on sc.scenario_id = ts.scenario_id "
			+ "inner join perf_projectname p on p.project_id=ts.project_id "
			+ "inner join perf_scenario_type st on st.scenario_type_id = ts.scenario_type_id "
			+ "where p.project_id=d.PROJECT_ID AND ts.STARTTIME >= (SELECT TRUNC(SYSDATE, 'd') FROM dual)), "
			+ "LAST_UPDATE=CURRENT_DATE;";

	public DashboardJpaDao() {
		super(DashboardEntity.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.exigen.performance.test.webapp.dao.DashboardDao#updateDashboard()
	 */
	public void updateDashboard() {
		Query query = getEntityManager().createNativeQuery(
				"BEGIN " + query1 + " END;");
		query.executeUpdate();
	}

}
